#!/bin/sh

# run from directory where this script is
cd `echo $0 | sed 's/\(.*\)\/.*/\1/'` # extract pathname
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use PostProc codes to calculate the DOS of Ni."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x dos.x projwfc.x kvecs_FS.x bands_FS.x"
PSEUDO_LIST="Ni.pz-nd-rrkjus.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for gnuplot
GP_COMMAND=`which gnuplot 2>/dev/null`
if [ "$GP_COMMAND" = "" ]; then
        $ECHO
        $ECHO "gnuplot not in PATH"
        $ECHO "Results will not be plotted"
fi
 
# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE \
                http://www.quantum-espresso.org/pseudo/1.3/UPF/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
DOS_COMMAND="$PARA_PREFIX $BIN_DIR/dos.x $PARA_POSTFIX"
PROJWFC_COMMAND="$PARA_PREFIX $BIN_DIR/projwfc.x $PARA_POSTFIX"
KVECS_COMMAND="$BIN_DIR/kvecs_FS.x "
BANDS_COMMAND="$BIN_DIR/bands_FS.x "
$ECHO
$ECHO "  running pw.x as:      $PW_COMMAND"
$ECHO "  running dos.x as:     $DOS_COMMAND"
$ECHO "  running projwfc.x as: $PROJWFC_COMMAND"
$ECHO "  running gnuplot as:   $GP_COMMAND"
$ECHO "  running kvecs_FS.x as:  $KVECS_COMMAND"
$ECHO "  running bands_FS.x as:  $BANDS_COMMAND"
$ECHO
 
# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

# self-consistent calculation
cat > ni.scf.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
                    <property name="starting_magnetization">
			<real>0.7</real>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				methfessel-paxton
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.02
			</real>
		</parameter>
		
	</field>	
	
	<field name="Fields">
		
		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="60">
			<real rank="2" n1="4" n2="60">
				0.0625000  0.0625000  0.0625000   1.00
			    0.0625000  0.0625000  0.1875000   3.00
			    0.0625000  0.0625000  0.3125000   3.00
			    0.0625000  0.0625000  0.4375000   3.00
			    0.0625000  0.0625000  0.5625000   3.00
			    0.0625000  0.0625000  0.6875000   3.00
			    0.0625000  0.0625000  0.8125000   3.00
			    0.0625000  0.0625000  0.9375000   3.00
			    0.0625000  0.1875000  0.1875000   3.00
			    0.0625000  0.1875000  0.3125000   6.00
			    0.0625000  0.1875000  0.4375000   6.00
			    0.0625000  0.1875000  0.5625000   6.00
			    0.0625000  0.1875000  0.6875000   6.00
			    0.0625000  0.1875000  0.8125000   6.00
			    0.0625000  0.1875000  0.9375000   6.00
			    0.0625000  0.3125000  0.3125000   3.00
			    0.0625000  0.3125000  0.4375000   6.00
			    0.0625000  0.3125000  0.5625000   6.00
			    0.0625000  0.3125000  0.6875000   6.00
			    0.0625000  0.3125000  0.8125000   6.00
			    0.0625000  0.3125000  0.9375000   6.00
			    0.0625000  0.4375000  0.4375000   3.00
			    0.0625000  0.4375000  0.5625000   6.00
			    0.0625000  0.4375000  0.6875000   6.00
			    0.0625000  0.4375000  0.8125000   6.00
			    0.0625000  0.4375000  0.9375000   6.00
			    0.0625000  0.5625000  0.5625000   3.00
			    0.0625000  0.5625000  0.6875000   6.00
			    0.0625000  0.5625000  0.8125000   6.00
			    0.0625000  0.6875000  0.6875000   3.00
			    0.0625000  0.6875000  0.8125000   6.00
			    0.0625000  0.8125000  0.8125000   3.00
			    0.1875000  0.1875000  0.1875000   1.00
			    0.1875000  0.1875000  0.3125000   3.00
			    0.1875000  0.1875000  0.4375000   3.00
			    0.1875000  0.1875000  0.5625000   3.00
			    0.1875000  0.1875000  0.6875000   3.00
			    0.1875000  0.1875000  0.8125000   3.00
			    0.1875000  0.3125000  0.3125000   3.00
			    0.1875000  0.3125000  0.4375000   6.00
			    0.1875000  0.3125000  0.5625000   6.00
			    0.1875000  0.3125000  0.6875000   6.00
			    0.1875000  0.3125000  0.8125000   6.00
			    0.1875000  0.4375000  0.4375000   3.00
			    0.1875000  0.4375000  0.5625000   6.00
			    0.1875000  0.4375000  0.6875000   6.00
			    0.1875000  0.4375000  0.8125000   6.00
			    0.1875000  0.5625000  0.5625000   3.00
			    0.1875000  0.5625000  0.6875000   6.00
			    0.1875000  0.6875000  0.6875000   3.00
			    0.3125000  0.3125000  0.3125000   1.00
			    0.3125000  0.3125000  0.4375000   3.00
			    0.3125000  0.3125000  0.5625000   3.00
			    0.3125000  0.3125000  0.6875000   3.00
			    0.3125000  0.4375000  0.4375000   3.00
			    0.3125000  0.4375000  0.5625000   6.00
			    0.3125000  0.4375000  0.6875000   6.00
			    0.3125000  0.5625000  0.5625000   3.00
			    0.4375000  0.4375000  0.4375000   1.00
			    0.4375000  0.4375000  0.5625000   3.00
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation for Ni...\c"
$PW_COMMAND < ni.scf.xml > ni.scf.out
check_failure $?
$ECHO " done"

# band structure calculation along Delta and Sigma lines
cat > ni.band.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="bands" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
                    <property name="starting_magnetization">
			<real>0.7</real>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>
		
		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
				
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				methfessel-paxton
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.02
			</real>
		</parameter>		
	
	</field>	
	
	<field name="Fields">
		
		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>
	
	<k_points type="tpiba">
		<mesh npoints="97">
			<real rank="2" n1="4" n2="97">
				1.000000000 0.000000000 0.000000000 1
				0.975000000 0.000000000 0.000000000 2
				0.950000000 0.000000000 0.000000000 3
				0.925000000 0.000000000 0.000000000 4
				0.900000000 0.000000000 0.000000000 5
				0.875000000 0.000000000 0.000000000 6
				0.850000000 0.000000000 0.000000000 7
				0.825000000 0.000000000 0.000000000 8
				0.800000000 0.000000000 0.000000000 9
				0.775000000 0.000000000 0.000000000 10
				0.750000000 0.000000000 0.000000000 11
				0.725000000 0.000000000 0.000000000 12
				0.700000000 0.000000000 0.000000000 13
				0.675000000 0.000000000 0.000000000 14
				0.650000000 0.000000000 0.000000000 15
				0.625000000 0.000000000 0.000000000 16
				0.600000000 0.000000000 0.000000000 17
				0.575000000 0.000000000 0.000000000 18
				0.550000000 0.000000000 0.000000000 19
				0.525000000 0.000000000 0.000000000 20
				0.500000000 0.000000000 0.000000000 21
				0.475000000 0.000000000 0.000000000 22
				0.450000000 0.000000000 0.000000000 23
				0.425000000 0.000000000 0.000000000 24
				0.400000000 0.000000000 0.000000000 25
				0.375000000 0.000000000 0.000000000 26
				0.350000000 0.000000000 0.000000000 27
				0.325000000 0.000000000 0.000000000 28
				0.300000000 0.000000000 0.000000000 29
				0.275000000 0.000000000 0.000000000 30
				0.250000000 0.000000000 0.000000000 31
				0.225000000 0.000000000 0.000000000 32
				0.200000000 0.000000000 0.000000000 33
				0.175000000 0.000000000 0.000000000 34
				0.150000000 0.000000000 0.000000000 35
				0.125000000 0.000000000 0.000000000 36
				0.100000000 0.000000000 0.000000000 37
				0.075000000 0.000000000 0.000000000 38
				0.050000000 0.000000000 0.000000000 39
				0.025000000 0.000000000 0.000000000 40
				0.000000000 0.000000000 0.000000000 41
				0.017857142 0.017857142 0.000000000 42
				0.035714285 0.035714285 0.000000000 43
				0.053571428 0.053571428 0.000000000 44
				0.071428571 0.071428571 0.000000000 45
				0.089285714 0.089285714 0.000000000 46
				0.107142857 0.107142857 0.000000000 47
				0.125000000 0.125000000 0.000000000 48
				0.142857142 0.142857142 0.000000000 49
				0.160714285 0.160714285 0.000000000 50
				0.178571428 0.178571428 0.000000000 51
				0.196428571 0.196428571 0.000000000 52
				0.214285714 0.214285714 0.000000000 53
				0.232142857 0.232142857 0.000000000 54
				0.250000000 0.250000000 0.000000000 55
				0.267857142 0.267857142 0.000000000 56
				0.285714285 0.285714285 0.000000000 57
				0.303571428 0.303571428 0.000000000 58
				0.321428571 0.321428571 0.000000000 59
				0.339285714 0.339285714 0.000000000 60
				0.357142857 0.357142857 0.000000000 61
				0.375000000 0.375000000 0.000000000 62
				0.392857142 0.392857142 0.000000000 63
				0.410714285 0.410714285 0.000000000 64
				0.428571428 0.428571428 0.000000000 65
				0.446428571 0.446428571 0.000000000 66
				0.464285714 0.464285714 0.000000000 67
				0.482142857 0.482142857 0.000000000 68
				0.500000000 0.500000000 0.000000000 69
				0.517857142 0.517857142 0.000000000 70
				0.535714285 0.535714285 0.000000000 71
				0.553571428 0.553571428 0.000000000 72
				0.571428571 0.571428571 0.000000000 73
				0.589285714 0.589285714 0.000000000 74
				0.607142857 0.607142857 0.000000000 75
				0.625000000 0.625000000 0.000000000 76
				0.642857142 0.642857142 0.000000000 77
				0.660714285 0.660714285 0.000000000 78
				0.678571428 0.678571428 0.000000000 79
				0.696428571 0.696428571 0.000000000 80
				0.714285714 0.714285714 0.000000000 81
				0.732142857 0.732142857 0.000000000 82
				0.750000000 0.750000000 0.000000000 83
				0.767857142 0.767857142 0.000000000 84
				0.785714285 0.785714285 0.000000000 85
				0.803571428 0.803571428 0.000000000 86
				0.821428571 0.821428571 0.000000000 87
				0.839285714 0.839285714 0.000000000 88
				0.857142857 0.857142857 0.000000000 89
				0.875000000 0.875000000 0.000000000 90
				0.892857142 0.892857142 0.000000000 91
				0.910714285 0.910714285 0.000000000 92
				0.928571428 0.928571428 0.000000000 93
				0.946428571 0.946428571 0.000000000 94
				0.964285714 0.964285714 0.000000000 95
				0.982142857 0.982142857 0.000000000 96
				1.000000000 1.000000000 0.000000000 97
			</real>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the band-structure calculation for Ni...\c"
$PW_COMMAND < ni.band.xml > ni.band.out
check_failure $?
$ECHO " done"

# K-resolved PDOS calculation along Delta and Sigma lines computed above
cat > ni.kpdos.in << EOF
 &inputpp
    outdir='$TMP_DIR/'
    prefix='ni'
    ngauss=0, degauss=0.036748
    DeltaE=0.01
    kresolveddos=.true.
    filpdos='ni.k'
 /
EOF
$ECHO "  running k-resolved PDOS calculation for Ni...\c"
$PROJWFC_COMMAND < ni.kpdos.in > ni.kpdos.out
check_failure $?
$ECHO " done"

#
#  if gnuplot was found, the results are plotted
#
if [ "$GP_COMMAND" = "" ]; then
    break
else
cat > gnuplot.tmp <<EOF
#!$GP_COMMAND
#
set term png enh size 1000,500
set pm3d
set view 0,0
#
f(z)=z**(0.7)  # tune image contrast
ef=15.2874
#
unset xtics
set xtics out nomirror ("X" 1,"Gamma" 41,"K"97)
set xra[1:97]
set label 1 "E-E_F(eV)" at 98,2.5
set ytics out nomirror
set yra [-10.9:20.9]
unset ztics
unset key
unset colorbox
#
set out 'kpdos_up.png'
set origin 0,0
set size 1,1
set multiplot
dx=.1 ; dy=.30   # reduce margins
set title 0,-7
set size 1./3+1.4*dx,1.+2*dy
set origin 0./3-dx,0-dy
set title "Total DOS"
splot 'ni.k.pdos_tot' u 1:(\$2-ef):(f(\$3)) w pm3d
set origin 1./3-dx,0-dy
set title "s-DOS"
splot 'ni.k.pdos_atm#1(Ni)_wfc#1(s)' u 1:(\$2-ef):(f(\$3)) w pm3d
set origin 2./3-dx,0-dy
set title "d-DOS"
splot 'ni.k.pdos_atm#1(Ni)_wfc#2(d)' u 1:(\$2-ef):(f(\$3)) w pm3d
unset multiplot
#
set out 'kpdos_dw.png'
set origin 0,0
set size 1,1
set multiplot
dx=.1 ; dy=.30   # reduce margins
set title 0,-7
set size 1./3+1.4*dx,1.+2*dy
set origin 0./3-dx,0-dy
set title "Total DOS"
splot 'ni.k.pdos_tot' u 1:(\$2-ef):(f(\$4)) w pm3d
set origin 1./3-dx,0-dy
set title "s-DOS"
splot 'ni.k.pdos_atm#1(Ni)_wfc#1(s)' u 1:(\$2-ef):(f(\$4)) w pm3d
set origin 2./3-dx,0-dy
set title "d-DOS"
splot 'ni.k.pdos_atm#1(Ni)_wfc#2(d)' u 1:(\$2-ef):(f(\$4)) w pm3d
unset multiplot
#
EOF
$ECHO
$ECHO "  plotting k-resolved DOS ...\c"
$GP_COMMAND < gnuplot.tmp
$ECHO " done"
rm gnuplot.tmp
fi

# DOS calculation for Ni
cat > ni.dos.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="nscf" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
                    <property name="starting_magnetization">
			<real>0.7</real>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">
	
		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				tetrahedra
			</string>
		</parameter>
		
		<parameter name="nbnd">
			<integer>
				8
			</integer>
		</parameter>
		
	</field>	
	
	<field name="Fields">
		
		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				 12 12 12 0 0 0
			</integer>
		</mesh> 
	</k_points>
</input>
EOF

cat > ni.dos2.in << EOF
 &inputpp
    outdir='$TMP_DIR/'
    prefix='ni'
    fildos='ni.dos',
    Emin=5.0, Emax=25.0, DeltaE=0.1
 /
EOF

$ECHO "  running DOS calculation for Ni...\c"
$PW_COMMAND < ni.dos.xml > ni.dos.out
check_failure $?
$DOS_COMMAND < ni.dos2.in > ni.dos2.out
check_failure $?
$ECHO " done"

cat > ni.pdos.in << EOF
 &inputpp
    outdir='$TMP_DIR/'
    prefix='ni'
    Emin=5.0, Emax=25.0, DeltaE=0.1
    ngauss=1, degauss=0.02
 /
EOF
$ECHO "  running PDOS calculation for Ni...\c"
$PROJWFC_COMMAND < ni.pdos.in > ni.pdos.out
check_failure $?
$ECHO " done"

$ECHO "  Fermi Surface plot Spin-Polarized case...\c"

# self-consistent calculation - Spin-Polarized (SP) case

cat > ni.scf_SP.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
                    <property name="starting_magnetization">
			<real>0.8</real>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				methfessel-paxton
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.02
			</real>
		</parameter>
		
	</field>	
	
	<field name="Fields">
		
		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				 8 8 8 0 0 0 
			</integer>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation  spin-polarization ... \c"
$PW_COMMAND < ni.scf_SP.xml > ni.scf0.SP.out
check_failure $?
$ECHO " done"

#
# prepare input file ni.fs_SP.in
#
Sysname='ni'
nabc=' 16 16 16 '
n_start=3
n_last=6
#
E_Fermi=`grep Fermi ni.scf0.SP.out | cut -c 26-36` 
a1=`grep 'b(1)' ni.scf.out | cut -c 24-54`
a2=`grep 'b(2)' ni.scf.out | cut -c 24-54`
a3=`grep 'b(3)' ni.scf.out | cut -c 24-54`

cat > kvecs_FS.in <<EOF
$a1
$a2
$a3
$nabc
$Sysname
EOF

$KVECS_COMMAND < kvecs_FS.in > kvecs_FS.out
check_failure $?

npoints=`head -n 1 kvecs_$Sysname | tr -d ' '`
points=`sed -n -e '2,$p' kvecs_$Sysname`


cat > ni.fs_SP.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="nscf" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
                    <property name="starting_magnetization">
			<real>0.8</real>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">
	
		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
		<parameter name="diagonalization">
			<string>
				cg
			</string>
		</parameter>
		
	</field>
	
	<field name="Options">
		
		<parameter name="nbnd">
			<integer>
				8
			</integer>
		</parameter>
		
	</field>
	
	<field name="Fields">
		
		<parameter name="nspin">
			<integer>
				2
			</integer>
		</parameter>
	
	</field>	
	
	<k_points type="tpiba">	
                <mesh npoints="$npoints">
                        <real rank="2" n1="4" n2="$npoints">
$points
                        </real>
                </mesh>
	</k_points>
</input>
EOF



# input file ni.fs.in ready
$ECHO "  running the Fermi Surface calculation ... \c"
$PW_COMMAND < ni.fs_SP.xml > ni.fs.SP.out 
check_failure $?
$ECHO " done"

# prepare input data (input_FS, Bands.out) for bands_FS

mv ni.fs.SP.out Bands_SP.out

cat > input_FS <<EOF
$n_start  $n_last
$E_Fermi
$Sysname
$nabc
$a1
$a2
$a3
EOF

$BANDS_COMMAND <Bands_SP.out  > bands_fs.out
check_failure $?
mv Bands_FS_up.bxsf    $Sysname.fs_up.bxsf 
mv Bands_FS_down.bxsf  $Sysname.fs_down.bxsf 

##################

$ECHO "  Fermi Surface plot Non-Spin-Polarized (NSP) case...\c"

# self-consistent calculation - Non-Spin-Polarised case

cat > ni.scf_NSP.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="scf" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">

		<parameter name="restart_mode">
			<string>
				from_scratch
			</string>
		</parameter>

		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
	</field>
	
	<field name="Options">

		<parameter name="occupations">
			<string>
				smearing
			</string>
		</parameter>
		
		<parameter name="smearing">
			<string>
				methfessel-paxton
			</string>
		</parameter>
		
		<parameter name="degauss">
			<real>
				0.02
			</real>
		</parameter>
		
	</field>	
	
	<k_points type="automatic">
		<mesh>
			<integer rank="1" n1="6">
				 8 8 8 0 0 0 
			</integer>
		</mesh> 
	</k_points>
</input>
EOF
$ECHO "  running the scf calculation  non spin-polarized case ... \c"
$PW_COMMAND < ni.scf_NSP.xml > ni.scf0.NSP.out
check_failure $?
$ECHO " done"

#
# prepare input file ni.fs.in
#
Sysname='ni'
nabc=' 16 16 16 '
n_start=3
n_last=6
#
E_Fermi=`grep Fermi ni.scf0.NSP.out | cut -c 26-36` 
a1=`grep 'b(1)' ni.scf.out | cut -c 24-54`
a2=`grep 'b(2)' ni.scf.out | cut -c 24-54`
a3=`grep 'b(3)' ni.scf.out | cut -c 24-54`

cat > kvecs_FS.in <<EOF
$a1
$a2
$a3
$nabc
$Sysname
EOF

$KVECS_COMMAND < kvecs_FS.in > kvecs_FS.out

npoints=`head -n 1 kvecs_$Sysname | tr -d ' '`
points=`sed -n -e '2,$p' kvecs_$Sysname`

cat > ni.fs_NSP.xml << EOF
<?xml version="1.0" encoding="UTF-8"?>

<input calculation="nscf" prefix="ni">

	<cell type="qecell">
		<qecell ibrav="2" alat="6.48">
			<real rank="1" n1="5">
				0.0 0.0 0.0 0.0 0.0
			</real>
		</qecell>
	</cell>

	<atomic_species ntyp="1">
		<specie name="Ni">
			<property name="mass">
				<real>58.69</real>
			</property>
                    <property name="pseudofile">
			<string>Ni.pz-nd-rrkjus.UPF</string>
                    </property>
		</specie>
	</atomic_species>

	<atomic_list units="alat" nat="1" >
		<atom name="Ni">
                     <position>
			<real rank="1" n1="3">
				0.0 0.0 0.0
			</real>
                     </position>
		</atom>						
	</atomic_list>		
	
	
	<field name="InputOutput">
	
		<parameter name="pseudo_dir">
			<string>
				$PSEUDO_DIR/
			</string>
		</parameter>
		
		<parameter name="outdir">
			<string>
				$TMP_DIR/
			</string>
		</parameter>
		
	</field>
	
	<field name="Numerics">

		<parameter name="ecutwfc">
			<real>
				24.0
			</real>
		</parameter>
		
		<parameter name="ecutrho">
			<real>
				288.0
			</real>
		</parameter>
		
		<parameter name="mixing_beta">
			<real>
				0.7
			</real>
		</parameter>
		
		<parameter name="conv_thr">
			<real>
				1.0e-10
			</real>
		</parameter>
		
		<parameter name="diagonalization">
			<string>
				cg
			</string>
		</parameter>
		
	</field>
	
	<field name="Options">
		
		<parameter name="nbnd">
			<integer>
				8
			</integer>
		</parameter>
		
	</field>	
	
	<k_points type="tpiba">	
                <mesh npoints="$npoints">
                        <real rank="2" n1="4" n2="$npoints">
$points
                        </real>
                </mesh>
	</k_points>
</input>
EOF


# input file ni.fs.in ready
$ECHO "  running the Fermi Surface calculation ... \c"
$PW_COMMAND < ni.fs_NSP.xml > ni.fs_NSP.out 
check_failure $?
$ECHO " done"

# prepare input data (input_FS, Bands.out) for bands_FS

mv ni.fs_NSP.out Bands_NSP.out

cat > input_FS <<EOF
$n_start  $n_last
$E_Fermi
$Sysname
$nabc
$a1
$a2
$a3
EOF

$BANDS_COMMAND < Bands_NSP.out > bands_fs.out
check_failure $?
mv Bands_FS.bxsf  ni.fs_NSP.bxsf 

$ECHO "  Fermi surface plot: use 'xcrysden --bxsf ni.fs_NSP.bxsf' to plot ...\c"
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR: done"
